package com.oepm.web.controller;

import com.oepm.biz.AcademyBiz;
import com.oepm.biz.DefenceBiz;
import com.oepm.common.entity.Meta;
import com.oepm.common.entity.Result;
import com.oepm.common.utils.JWTUtil;
import com.oepm.dao.entity.AcademysPo;
import com.oepm.dao.entity.DefencePo;
import com.oepm.dao.entity.QueryInfo;
import com.oepm.vo.DataVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/defence")
public class DefenceController {
    @Autowired
    private DefenceBiz defenceBiz;
    @Autowired
    private AcademyBiz academyBiz;

    /**
     * 答辩管理--添加
     * @param defence
     * @return
     */
    @PostMapping("/insert")
    @RequiresRoles(logical = Logical.OR,value = {"1","2"})
    public Result insert(@RequestBody DefencePo defence){
        if (defence.getLevel().equals("重点") & defence.getName().equals("结题答辩")){
            defenceBiz.deleteByAcademy(defence.getAcademy());
        }
        int flag = defenceBiz.insert(defence);
        Result result;
        if (flag>0){
            result = new Result(Meta.CREATED);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }


    /**
     * 答辩管理--删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @RequiresRoles(logical = Logical.OR,value = {"1","2"})
    public Result delete(@PathVariable String id){
        int flag = defenceBiz.delete(id);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    /**
     * 答辩管理--更新
     * @param defence
     * @return
     */
    @PostMapping("/update")
    @RequiresRoles(logical = Logical.OR,value = {"1","2"})
    public Result update(@RequestBody DefencePo defence){
        int flag = defenceBiz.update(defence);
        Result result;
        if (flag>0){
            result = new Result(Meta.SUCCESS);
        }else{
            result = new Result(Meta.FAIL);
        }
        return result;
    }

    /**
     * 查询学院
     * @return
     */
    @GetMapping("/getAcademy")
    @RequiresRoles(logical = Logical.OR,value = {"1","2"})
    public Result selectAcademy(){
        List<AcademysPo> list = academyBiz.selectAcademy();
        Result result = new Result(Meta.SUCCESS,list);
        return result;
    }

    /**
     * 答辩管理--查询
     * @param request
     * @param queryInfo
     * @return
     */
    @GetMapping("/select")
    public Result select(HttpServletRequest request, QueryInfo queryInfo){
        String token=request.getHeader("Authorization");
        String role = JWTUtil.getRoleId(token);
        String academy = JWTUtil.getAcademy(token);
        Result result;
        if (role.equals("1")){
            DataVo dataVo = defenceBiz.selectByLevel(queryInfo,"重点");
            result = new Result(Meta.SUCCESS);
            result.setData(dataVo);
        }else if(role.equals("2")){
            DataVo dataVo = defenceBiz.selectPu(queryInfo,academy);
            result = new Result(Meta.SUCCESS);
            result.setData(dataVo);
        }else{
            DataVo dataVo = defenceBiz.selectByAcademy(queryInfo,academy);
            result = new Result(Meta.SUCCESS);
            result.setData(dataVo);
        }
        return result;
    }
}
